package org.openfidias.jdbase.controller;

import java.io.IOException;
import org.apache.commons.lang.StringUtils;
import org.openfidias.jdbase.locale.Message;
import org.openfidias.jdbase.model.Aluno;
import org.openfidias.jdbase.model.AlunoDao;
import org.openfidias.jdbase.model.AlunoDisciplina;
import org.openfidias.jdbase.model.AlunoDisciplinaDao;
import org.openfidias.jdbase.model.Disciplina;
import org.openfidias.jdbase.template.IController;
import org.openfidias.jdbase.view.Console;

/**
 *
 * @author atila
 */
public class AlunoDisciplinaBo implements IController {

   private AlunoDisciplinaBo() {
   }

   public static AlunoDisciplinaBo getInstance() {
      return AlunoDisciplinaBoHolder.INSTANCE;
   }

   private static class AlunoDisciplinaBoHolder {

      private static final AlunoDisciplinaBo INSTANCE = new AlunoDisciplinaBo();
   }

   public void create() throws IOException, Exception {
      AlunoDisciplina ad = new AlunoDisciplina();
      System.out.println(StringUtils.center(" Criar Aluno_Disciplina ", 50, "·"));
      String value = "";

      // setting Login(Aluno)
      while (true) {

         System.out.println("Digite o Login(Aluno): ");
         System.out.print(Console.bash);
         value = Console.read.nextLine();
         // confirm the Login
         Aluno aluno = AlunoDao.getInstance().read(value);
         if ("".equals(aluno.getLogin())) {
            System.out.println(Message.notFound("Aluno"));
            continue;
         } else {
            System.out.println(Aluno.headerTable());
            System.out.println(aluno);
            System.out.println(Aluno.rowSeparator());

            System.out.println("Confirmar? [y/n]");
            value = Console.read.nextLine();
            if ("y".equalsIgnoreCase(value)) {
               ad.setAluno(aluno);
               break;
            } else {
               System.out.println("Cancelado pelo usuário.");
               continue;
            }
         }

      } // end of while

      // setting the Codigo(Disciplina)
      while (true) {

         System.out.println("Digite o Código(Disciplina): ");
         System.out.print(Console.bash);
         value = Console.read.nextLine();
         // verifica numeric
         if (!StringUtils.isNumeric(value)) {
            System.out.println(Message.onlyNumeric("Código"));
            continue;
         }
         // confirm the Codigo
         Disciplina disciplina = new Disciplina(Integer.parseInt(value));
         // TODO: não permitir cadastro de disciplinas com codigo 0(zero)!
         if (disciplina.getCodigo() <= 0) {
            System.out.println(Message.notFound("Disciplina"));
            continue;
         } else {
            System.out.println(Disciplina.headerTable());
            System.out.println(disciplina);
            System.out.println(Disciplina.footerTable());

            System.out.println("Confirmar? [y/n]");
            value = Console.read.nextLine();
            if ("y".equalsIgnoreCase(value)) {
               ad.setDisciplina(disciplina);
               break;
            } else {
               System.out.println("Cancelado pelo usuário.");
               continue;
            }
         }

      } // end of while

      // saving to the database
      try {
         AlunoDisciplinaDao.getInstance().create(ad);
      } catch (Exception e) {
         System.out.println(Message.showError(e));
      }
   }

   public void read() {
      throw new UnsupportedOperationException("Not supported yet.");
   }

   public void delete() throws IOException, Exception {
      throw new UnsupportedOperationException("Not supported yet.");
   }
}
